package com.bocom.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.bocom.entity.AjfxEntity;
import com.bocom.service.AjfxService;

@Controller
@SessionAttributes("link")
public class AjfxControl {

	@Resource(name = "ajfxService")
	private AjfxService service;

	@RequestMapping(value = "jsp/SelectAjfx", method = RequestMethod.GET)
	public String select_kssj(ModelMap modelMap, @RequestParam String kssj,
			@RequestParam String jssj) {

//		System.out.println("AjfxControl kssj=" + kssj);
		List<AjfxEntity> list = service.select_jobsubmit(kssj, jssj);
		modelMap.put("list", list);
		modelMap.addAttribute("kssj", kssj);
		modelMap.addAttribute("jssj", jssj);
		return "../jsp/loginform.jsp";

	}


	@RequestMapping(value = "jsp/excel", method = RequestMethod.GET)
	public String select_excel(@RequestParam String kssj,
			@RequestParam String jssj,HttpServletRequest request,ModelMap modelMap) {

		String dir = request.getSession().getServletContext().getRealPath("/"+"download");
		String path = request.getContextPath();  
		String serverpath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/download/";  
//		System.out.println("dir=" + dir);
//		System.out.println("serverpath=" + serverpath);
		List<AjfxEntity> list = service.select_jobsubmit(kssj, jssj);
		putOutTaskToExcelFile(list,dir);
		String serverfile = serverpath + "/ptga.csv";
//		System.out.println("excel=" + serverfile);
		modelMap.put("link", serverfile);
		return "../jsp/download.jsp";
		
	}	
	

	public File putOutTaskToExcelFile(List<AjfxEntity> getPutOutTaskResult,String dir) {
		//在我的代码里，getPutOutTaskResult是我需要导出的信息列表，你换成你的就行
		BufferedWriter out = null;
		File excelFile = null;
		//导出的文件名
		excelFile = new File(dir+"/"+"ptga"  +".csv");
//		System.out.println("putOutTaskToExcelFile=" + excelFile);
		//生成一个csv临时文件
		excelFile.deleteOnExit();
//		int i = 1;
		try {

			out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(excelFile), "GBK"));

			// excel表头
			out.write("序号" + "," + "任务下发日期" + "," + "资源上传单位" + "," + "资源收集日期"
					+ "," + "案件编号" + "," + "案发日期" + "," + "案发时段" + ","
					+ "案事件类别" + "," + "案发场所" + "," + "作案手法" + "," + "嫌疑车辆"
					+ "," + "车辆类型" + "," + "车辆颜色" + "," + "车牌号" + "," + "案发地址"
					+ "," + "案事件备注" + ",");
			out.newLine();
			Iterator<AjfxEntity> resultIterator = getPutOutTaskResult.iterator();
			while (resultIterator.hasNext()) {
				AjfxEntity e = resultIterator.next();

				// excel对应的列
				out.write(e.getRn() + "," + e.getJobsubmitdate() + "," + e.getJobowner()
						+ "," + e.getJobsubmitdate() + "," + e.getCaseid()
						+ "," + e.getCasedate() + "," + e.getCasetime() + ","
						+ e.getCasetype() + "," + e.getCaseposition() + ","
						+ e.getCasemethod() + "," + e.getHassuspectveicle()
						+ "," + e.getSuspectveicletype() + ","
						+ e.getSuspectveiclecolor() + ","
						+ e.getSuspectveiclecard() + "," + e.getCaseaddress()
						+ "," + e.getCaseremark().replaceAll("\r|\n", " ") + ",");
				out.newLine();
//				i++;
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return excelFile;
	}

}
